package com.lyw.forum.controller;

import com.lyw.forum.common.enums.ResultCode;
import com.lyw.forum.pojo.request.ModifyPasswordRequest;
import com.lyw.forum.pojo.request.UserLoginRequest;
import com.lyw.forum.pojo.request.UserModifyRequest;
import com.lyw.forum.pojo.request.UserRegisterRequest;
import com.lyw.forum.pojo.response.AppResult;
import com.lyw.forum.pojo.response.UserLoginResponse;
import com.lyw.forum.pojo.response.UserSelectResponse;
import com.lyw.forum.service.IUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lyw
 */

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService UserServiceImpl;

    @RequestMapping("/register")
    public AppResult register(@RequestBody @Validated UserRegisterRequest userRegisterRequest) {
        //校验两次密码是否输入的相同
        if (!userRegisterRequest.getPassword().equals(userRegisterRequest.getRepeatPassword())) {
            //打印日志
            log.error(ResultCode.FAILED_TWO_PWD_NOT_SAME.toString());
            //返回错误信息
            return AppResult.fail(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }

        //调用业务逻辑
        UserServiceImpl.createNormalUser(userRegisterRequest);

        return AppResult.success();
    }

    @RequestMapping("/login")
    public AppResult login(@RequestBody @Validated UserLoginRequest userLoginRequest) {
        //1.非空校验
        if (userLoginRequest == null || !StringUtils.hasLength(userLoginRequest.getUsername()) || !StringUtils.hasLength(userLoginRequest.getPassword())) {
            //打印日志
            log.error("用户名或密码不能为空");
            //返回错误信息
            return AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //2.调用service中的登录方法, 返回UserLoginResponse对象
        UserLoginResponse login = UserServiceImpl.login(userLoginRequest);

        //3.返回结果
        return AppResult.success(login);
    }

    @RequestMapping("/select")
    public AppResult getUserInfo(@RequestParam(value = "id", required = false) Integer id, HttpServletRequest request) {
        //判断id是否为空
        UserSelectResponse userSelectResponse = null;
        if (id == null) {
            String userId = request.getHeader("id");
            //根据id查询用户信息
            userSelectResponse = UserServiceImpl.selectById(Integer.parseInt(userId));
        } else {
            //根据id查询用户信息
            userSelectResponse = UserServiceImpl.selectById(id);
        }

        //判断用户信息是否为空
        if (userSelectResponse == null) {
            return AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }

        //返回结果
        return AppResult.success(userSelectResponse);
    }

    @RequestMapping("/modifyInfo")
    public AppResult modifyUserInfo(@RequestBody @Validated UserModifyRequest userModifyRequest, HttpServletRequest request) {

        //对参数进行非空校验
        if (userModifyRequest.getUsername() == null && userModifyRequest.getNickname() == null
                && userModifyRequest.getEmail() == null && userModifyRequest.getPhoneNum() == null
                && userModifyRequest.getRemark() == null && userModifyRequest.getGender() ==  null) {

            //返回错误信息
            return AppResult.fail("请输入要修改的内容");
        }

        //获取用户id
        String id = request.getHeader("id");

        //设置用户id
        userModifyRequest.setId(Integer.parseInt(id));

        //调用业务逻辑
        UserServiceImpl.modifyUserInfo(userModifyRequest);

        //把最新的用户信息返回
        UserSelectResponse userSelectResponse = UserServiceImpl.selectById(Integer.parseInt(id));

        //返回结果
        return AppResult.success(userSelectResponse);
    }

    @RequestMapping("/modifyPwd")
    public AppResult modifyPassword(@RequestBody @Validated ModifyPasswordRequest modifyPasswordRequest, HttpServletRequest request) {
        //两次密码是否一样
        if (!modifyPasswordRequest.getNewPassword().equals(modifyPasswordRequest.getPasswordRepeat())) {
            //返回错误描述
            return AppResult.fail(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }

        //获取用户id
        String id = request.getHeader("id");

        //设置用户id
        modifyPasswordRequest.setId(Integer.parseInt(id));

        //调用业务逻辑
        UserServiceImpl.modifyPassword(modifyPasswordRequest);

        //返回结果
        return AppResult.success();
    }
}
